<?php use_stylesheet('hoverTables.css') //load css    ?>
<?php use_stylesheet('forms.css') //load css pf order page   ?>
<div id="purchases" class="table_div">
    <table>
        <tr class="bold head">
            <td>Material No.</td>
            <td>Name</td>
            <td>Type</td>
            <td>Quantity</td>
            <td>Unit</td>
            <td>Supplier</td>
            <td>Price per Unit (excl. VAT)</td>
            <td>Last updated</td>
            <td>Status</td>
            <td width="35px"></td>
        </tr>
        <!-- Retrieve data of each purchase and put it in the table -->
        <?php foreach ($purchases as $i=>$purchase): ?>
        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?>">
            <?php $id = $purchase->getMaterialId() ?>
            <?php $material = PartTable::getInstance()->findOneBy('id', $id) ?>
            <td><?php echo $material->getPartNumber() ?></td>
            <td><?php echo $material->getName() ?></td>
            <td><?php echo $material->getType() ?></td>
            <td><?php echo $purchase->getAmount() ?></td>
            <td><?php echo $material->getUnit() ?></td>
            <!-- If this purchase has a supplier display the name, else display nothing -->
            <?php if($purchase->getSupplierId() == null){
                $supplier = null;}
                else{
                $supplier = ContactTable::getInstance()->findOneBy('id', $purchase->getSupplierId());} ?>
            <td><?php if($supplier == null){ echo null; }else{echo $supplier->getName();} ?></td>
            <td><?php echo $purchase->getPrice() ?></td>
            <td><?php echo $purchase->getUpdatedAt() ?></td>
            <td><?php echo $purchase->getStatus() ?></td>
            <!-- Only show an edit button if the status is 'ordered'-->
            <?php if($purchase->getStatus() == 'ordered'){ ?>
                <td><a href="<?php echo url_for('purchase/editPurchase?id='.$purchase->getId()) ?>"><img src="<?php echo image_path('Buttons/bt_edit_small.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_edit_small_hov.png') ?>';"  onmouseout="this.src='<?php echo image_path('Buttons/bt_edit_small.png') ?>';" title="Edit purchase"/></a></td>
            <?php } else { ?>
                <td></td>
            <?php } ?>
        </tr>
        <?php endforeach; ?>
    </table>
 </div>